"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectTaxpaymentCtrl
 * @description
 * # ProjectTaxpaymentCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('ProjectTaxpaymentCtrl', function ($scope,DataService, localStorageService, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
    var id = $scope.process_id = $routeParams.id
    $scope.showType = function () {
      var param = {}
      param.type = "税种";
      DataService.getData(openApi.getKeyType, param).then(function (data) {
        $scope.typeList = data
      })
    };
    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };
    $scope.chooseContract = function () {
      var params = $scope.param;
      if (!params.projectId || !params.contractType) {
        Util.Toast("请先选择项目与合同类型");
        return;
      }
      var projectParams = {
        formKey: params.contractType,
        fieldName: "contractNo",
        projectId: params.projectId,
        projectNo: params.projectNo
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.chooseSupplier = function () {
      var params = $scope.param;
      var projectParams = {
        formKey: "Xm_SupplierStore",
        fieldName: "supplierNo"
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };
    $scope.showCertificate = function () {
      $scope.showCerPicker = true;
      $rootScope.$emit("showmasker");
    };
    $scope.deletedItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.costindex = -1;
    };

    $scope.addCertificate = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      $scope.param.details.push($scope.picker);
      // 隐藏表单
      closePicker();
    };
    var closePicker = function () {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
    };
    $scope.$on("closemasker", function () {
      closePicker();
    });
    $scope.closePicker = function () {
      closePicker();
    };
    $scope.swipe = function (index) {
      $scope.costindex = index;
    };
    $scope.resetlist = function () {
      $scope.costindex = -1;
    };
    // 验证是否输入完整
    var checkPicker = function (data) {
      if (!data.type) {
        Util.Toast("请选择税种");
        return;
      }
      if (!data.amount) {
        Util.Toast("请填写税额");
        return;
      }

      return true;
    };

    $scope.submit = function (operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.addtaxesPayment;
      options.param = angular.copy($scope.param, {});
      options.param.taxDetail = JSON.stringify(options.param.details);

      delete options.param.details
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目名称");
        return;
      }

      if (!$scope.param.taxAuthority) {
        Util.Toast("请选填写税务机关");
        return;
      }
      if (!$scope.param.receiptQuantity) {
        Util.Toast("请填写税票数量");
        return;
      }

      if (!$scope.param.taxStartDate) {
        Util.Toast("请选择税款所属时期(开始)");
        return;
      }
      if (!$scope.param.taxEndDate) {
        Util.Toast("请选择税款所属时期(截止)");
        return;
      }
      if (!$scope.param.details) {
        Util.Toast("请填写税额明细");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
  
        $scope.param = Object.assign({}, $scope.param, data);
        $scope.param.details = $scope.param.details || []
      });
      $scope.showType()
    };
    // 初始化
    init();
  });